package com.sinosoft.hydra.um.domain.permission;

import cn.com.sinux.spring.utils.DateUtils;
import com.alibaba.fastjson.annotation.JSONField;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lcb on 2016/8/30.
 */
@Entity
@Table(name="T_ATTENDANCE")
public class TAttendance implements Serializable{

    public static final String PASS = "1";
    public static final String REFUSE = "0";
    public static final String WAIT = "2";

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name="ID")
    private String id;

    @OneToOne
    @JoinColumn(name="REASON")
    private TDirectory reason;

    @OneToOne
    @JoinColumn(name="APPLY_USER")
    private SysUserMember applyUser;

    @OneToOne
    @JoinColumn(name="CREATE_USER")
    private SysUserMember createUser;

    @Column(name="CREATE_TIME")
    @JSONField(format = "yyyy-MM-dd HH:mm")
    private Date createTime;

    @Column(name="IS_REPLACE")
    private int isReplace;

    @Column(name="BEGIN_TIME")
    @JSONField(format = "yyyy-MM-dd HH:mm")
    private Date beginTime;

    @Column(name="END_TIME")
    @JSONField(format = "yyyy-MM-dd HH:mm")
    private Date endTime;

    @Column(name="REMARK")
    private String remark;

    @Column(name="PROJECT_CODE")
    private String projectCode;

    @Column(name="APM")
    private String apm;

    @Column(name="DAYS")
    private String days;

    @Column(name="STATUS")
    private String status;

    @Transient
    public Date getHours(){
        if(this.endTime == null){
            return null;
        }
//        if(!Arrays.asList("公干", "零星事假", "加班").contains(this.reason.getDirValue())){
//            return null;
//        }
        if (StringUtils.isNotBlank(this.apm)){
            return null;
        }
        long ms = this.endTime.getTime() - this.beginTime.getTime();
        int hour = (int)(ms/(60*60*1000));
        int minute = (int)(ms/(60*1000)) - hour*60;
        return DateUtils.setMinutes(DateUtils.setHours(new Date(), hour), minute);
    }

    @Transient
    public String getHourOrDay(){
        if (this.endTime == null){
            return null;
        }
        if (StringUtils.isNotBlank(this.apm)){
            return this.days + "天";
        }else {
            long ms = this.endTime.getTime() - this.beginTime.getTime();
            int hour = (int)(ms/(60*60*1000));
            int minute = (int)(ms/(60*1000)) - hour*60;
            return minute != 0 ? hour + "小时" + minute + "分" : hour + "小时";
        }
    }

//    @Transient
//    public String getFromTo() {
//        return DateUtils.formatDate(beginTime, "HH:mm") + "~" +
//                DateUtils.formatDate(endTime, "HH:mm");
//    }
//
//    @Transient
//    public String getWorkHours(){
//        if(endTime == null){
//            return null;
//        }
//        long ms = endTime.getTime() - beginTime.getTime();
//        double hours = (double)ms/(60*60*1000);
//        return String.valueOf((double)Math.round(hours*10)/10);
//    }

    private static final long serialVersionUID = 1L;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public TDirectory getReason() {
        return reason;
    }

    public void setReason(TDirectory reason) {
        this.reason = reason;
    }

    public SysUserMember getApplyUser() {
        return applyUser;
    }

    public void setApplyUser(SysUserMember applyUser) {
        this.applyUser = applyUser;
    }

    public SysUserMember getCreateUser() {
        return createUser;
    }

    public void setCreateUser(SysUserMember createUser) {
        this.createUser = createUser;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getIsReplace() {
        return isReplace;
    }

    public void setIsReplace(int isReplace) {
        this.isReplace = isReplace;
    }

    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getProjectCode() {
        return projectCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    public String getApm() {
        return apm;
    }

    public void setApm(String apm) {
        this.apm = apm;
    }

    public String getDays() {
        return days;
    }

    public void setDays(String days) {
        this.days = days;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
